package sumlogic

import (
	"common/comRedis"
	"context"
	"fmt"
	"github.com/zeromicro/go-zero/core/logx"
	"service/internal/svc"
	"service/pb"
	"time"
)

type MyAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewMyAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MyAddLogic {
	return &MyAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

type Slew struct {
	Num int64 `json:"num"`
}

func (l *MyAddLogic) MyAdd(in *pb.SumRequest) (*pb.SumResponse, error) {
	// todo: add your logic here and delete this line
	//user, err := comJwt.BackUser(l.ctx, l.svcCtx.Config.USERDATA)
	//if err != nil {
	//	return nil, err
	//}
	prams := comRedis.RedisPrams{
		Rdb: l.svcCtx.Rdb,
		Ctx: l.ctx,
		Key: "user:" + "123",
		//Expiration: 1 * time.Minute,
		LockKey:        "userLock:" + "123",
		LockValue:      "123",
		LockExpiration: 1 * time.Minute,
	}
	redisStruct, err := comRedis.GetRedisStruct[Slew](&prams)
	if err != nil {
		return nil, err
	}
	redisStruct.Num += 1
	err = comRedis.SetRedisStruct(&prams, redisStruct)
	if err != nil {
		return nil, err
	}

	fmt.Println(redisStruct)
	resp := pb.SumResponse{}
	resp.Result = in.Numbers.A + in.Numbers.B
	return &resp, nil
}
